package org.example.netty.test.nio.netty.chat.bean;

import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.example.netty.test.nio.netty.chat.common.AgreeOnVersion;
import org.example.netty.test.nio.netty.chat.common.MagicNumber;
import org.example.netty.test.nio.netty.chat.common.MessageStatus;
import org.example.netty.test.nio.netty.chat.common.SerializationVersion;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;

/**
 * @author zp
 * @version 1.0
 * @description: 消息体
 * <p>
 * +---------------------------------------------------------------+
 * | 魔数 4byte | 协议版本号 4byte | 序列化算法 4byte | 报文类型 4byte  |
 * +---------------------------------------------------------------+
 * | 状态 4byte | 数据长度 4byte  | 发送人ID 4byte | 接收人ID（如果是群 群ID） 4byte|
 * +---------------------------------------------------------------+
 * |保留字段 4byte|数据内容 （长度不定）                                 |
 * +---------------------------------------------------------------+
 * @date 2023/3/30 19:41
 */
@Data
@Slf4j
@ToString
public abstract class AbsMessageBase implements Message,Serializable {
    protected String sender;
    protected String recipient;
    protected int messageStatus;
    protected String content;
    protected int messageType;

    public static int magicNumber = MagicNumber.MAGIC_NUMBER;
    private static int agreeOnVersion = AgreeOnVersion.VERSION;
    private static int serializationVersion = SerializationVersion.JDK_SERIALIZE;
    // 加载配置信息
    static {
        InputStream inputStream = AbsMessageBase.class.getResourceAsStream("/application.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String magicNumberConfig = properties.getProperty("message.MagicNumber");
        String agreeOnVersionConfig = properties.getProperty("message.AgreeOnVersion");
        String serializationVersionConfig = properties.getProperty("message.SerializationVersion");
        if(magicNumberConfig!=null){
            magicNumber = Integer.parseInt(magicNumberConfig);
        }
        if(agreeOnVersionConfig!=null){
            agreeOnVersion = Integer.parseInt(agreeOnVersionConfig);
        }
        if(serializationVersionConfig!=null){
            if(SerializationVersion.JSON_CODE.equals(serializationVersionConfig)){
                serializationVersion = SerializationVersion.JSON_SERIALIZE;
            }else if (SerializationVersion.JDK_CODE.equals(serializationVersionConfig)) {
                serializationVersion = SerializationVersion.JDK_SERIALIZE;
            }else {
                log.error("序列化方式配置错误");
            }
        }
    }


    /**
     * @return int
     * @description: 返回消息类
     * @author zp
     * @date: 2023/3/30 19:42
     */
    @Override
    public  int getMessageType(){
        return messageType;
    }

    /**
     * @return byte[]
     * @description: 获取消息状态
     * @author zp
     * @date: 2023/3/30 19:50
     */
    @Override
    public int messageTypeStatus() {
        return messageStatus;
    }

    /**
     * @description: 设置消息状态
     * @author zp
     * @date: 2023/4/13 19:01
     * @param messageStatus
     */
    public void setMessageTypeStatus(int messageStatus){
        this.messageStatus = messageStatus;
    }

    /**
     * @return byte[]
     * @description: 获取魔数
     * @author zp
     * @date: 2023/3/30 19:46
     */
    @Override
    public int getMagicNumber() {
        return magicNumber;
    }

    /**
     * @return byte[]
     * @description: 获取版本号
     * @author zp
     * @date: 2023/3/30 19:47
     */
    @Override
    public int getVersion() {
        return agreeOnVersion;
    }

    /**
     * @return byte[]
     * @description: 获取序列化算法
     * @author zp
     * @date: 2023/3/30 19:49
     */
    @Override
    public int getSerializationAlgorithm() {
        return serializationVersion;
    }

    @Override
    public String getMessage() {
        return content;
    }

}
